package com.jtny.nytb.equipment.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.jtny.component.entity.WebResponse;
import com.jtny.component.exception.ServiceException;
import com.jtny.nytb.equipment.service.DataAlgorithmBiz;
import com.jtny.nytb.equipment.vo.dataagorithm.*;
import com.jtny.nytb.config.excel.biz.ImportExcelBiz;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 数据算法管理Controller类
 * @package com.jtny.nytb.config.generic.dataalgorithm.controller
 * @fileName DataAlgorithmController.java
 * @author 智慧能源中心-夏燕强
 * @createTime 2023年09月01日 15:35
 * @version 1.0
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@RestController
@Api(value = "数据算法管理", tags = "数据算法管理")
@ApiSort(102022)
@RequestMapping("/config/dataAlgorithm")
@AllArgsConstructor
public class DataAlgorithmController {
    private DataAlgorithmBiz dataAlgorithmBiz;

    @ApiOperation("分页查询数据算法")
    @ApiImplicitParam(name = "dataAlgorithmPageQueryParamVO", value = "分页查询数据算法", required = true, paramType = "query", dataType = "DataAlgorithmPageQueryParamVO")
    @ApiOperationSupport(order = 1)
    @PostMapping("/queryPage")
    @ResponseBody
    public WebResponse<?> queryPage(@RequestBody DataAlgorithmPageQueryParamVO dataAlgorithmPageQueryParamVO) {
        return WebResponse.success("操作成功",dataAlgorithmBiz.queryPage(dataAlgorithmPageQueryParamVO));
    }

    @ApiOperation("保存数据算法")
    @ApiImplicitParam(name = "algorithmScriptAndFormulaVO", value = "保存数据算法", required = true, paramType = "query", dataType = "DataAlgorithmScriptAndFormulaVO")
    @ApiOperationSupport(order = 2)
    @PostMapping("/save")
    @ResponseBody
    public WebResponse<?> saveDataAlgorithm(@RequestBody @Valid DataAlgorithmScriptAndFormulaVO algorithmScriptAndFormulaVO) {
        try {
            return WebResponse.success("操作成功",dataAlgorithmBiz.saveDataAlgorithm(algorithmScriptAndFormulaVO));
        }catch (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("查看数据算法详情")
    @ApiImplicitParam(name = "algorithmId", value = "查看数据算法详情", required = true, paramType = "path")
    @ApiOperationSupport(order = 3)
    @GetMapping("/detail/{algorithmId}")
    @ResponseBody
    public WebResponse<?> detailDataAlgorithm(@PathVariable("algorithmId") Long algorithmId) {
        try {
            return WebResponse.success("操作成功",dataAlgorithmBiz.detailDataAlgorithm(algorithmId));
        }catch (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("修改数据算法")
    @ApiImplicitParam(name = "dataAlgorithmScriptAndFormulaVO", value = "修改数据算法", required = true, paramType = "query", dataType = "DataAlgorithmScriptAndFormulaVO")
    @ApiOperationSupport(order = 4)
    @PostMapping("/update")
    @ResponseBody
    public WebResponse<?> updateDataAlgorithm(@RequestBody @Valid DataAlgorithmScriptAndFormulaVO dataAlgorithmScriptAndFormulaVO) {
        try {
            dataAlgorithmBiz.updateDataAlgorithm(dataAlgorithmScriptAndFormulaVO);
            return  WebResponse.success("操作成功");
        }catch  (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("删除数据算法")
    @ApiImplicitParam(name = "algorithmId", value = "数据算法基本信息id", required = true, paramType = "path")
    @ApiOperationSupport(order = 5)
    @GetMapping("/delete/{algorithmId}")
    @ResponseBody
    public WebResponse<?> deleteDataAlgorithm(@PathVariable("algorithmId") Long algorithmId) {
        try {
            dataAlgorithmBiz.deleteDataAlgorithm(algorithmId);
            return  WebResponse.success("操作成功");
        }catch  (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("禁用数据算法")
    @ApiImplicitParam(name = "algorithmId", value = "禁用数据算法", required = true, paramType = "path")
    @ApiOperationSupport(order = 6)
    @GetMapping("/disable/{algorithmId}")
    @ResponseBody
    public WebResponse<?> disableDataAlgorithm(@PathVariable("algorithmId") Long algorithmId) {
        try {
            return WebResponse.success("操作成功",dataAlgorithmBiz.disableDataAlgorithm(algorithmId));
        }catch (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("启用数据算法")
    @ApiImplicitParam(name = "algorithmId", value = "启用数据算法", required = true, paramType = "path")
    @ApiOperationSupport(order = 7)
    @GetMapping("/enable/{algorithmId}")
    @ResponseBody
    public WebResponse<?> enableDataAlgorithm(@PathVariable("algorithmId") Long algorithmId) {
        try {
            return WebResponse.success("操作成功",dataAlgorithmBiz.enableDataAlgorithm(algorithmId));
        }catch (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("删除数据算法相关的参数配置信息")
    @ApiImplicitParam(name = "algorithmId", value = "删除数据算法相关的参数配置信息", required = true, paramType = "path")
    @ApiOperationSupport(order = 7)
    @GetMapping("/deleteParamConfig/{algorithmId}")
    @ResponseBody
    public WebResponse<?> deleteParamConfig(@PathVariable("algorithmId") Long algorithmId) {
        try {
            dataAlgorithmBiz.deleteParamConfig(algorithmId);
            return  WebResponse.success("操作成功");
        }catch (ServiceException e){
            return WebResponse.error(e);
        }
    }

//
//    @PostMapping("/export-template")
//    @ApiOperation(value = "获取导入模板")
//    public void exportTemplate(HttpServletResponse response) {
//        EasyExcelUtils.exportExcelManySheet(response, "算法导入模版", ExcelSheetUtil.buildAlgorithmExcelSheet());
//    }
//
//    @ApiOperation("导入算法")
//    @ApiImplicitParam(name = "file", value = "算法excel文件", required = true, paramType = "query", dataType = "MultipartFile")
//    @ApiOperationSupport(order = 9)
//    @PostMapping("/import")
////    @OperationLog(value = "导入算法")
//    @ResponseBody
//    public WebResponse<?> importAlgorithm(@RequestParam(value = "file") MultipartFile file) {
//        List<ExcelSheetVO> excelSheetList = ExcelSheetUtil.buildAlgorithmExcelSheet();
//        Map<Class<?>, List<?>> map = null;
//        try {
//            map = EasyExcelUtils.importExcelManySheet(file, excelSheetList);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        importExcelBiz.importAlgorithmExcel(map, excelSheetList);
//        return WebResponse.success();
//    }
//
//    @ApiOperation("导出算法")
//    @ApiOperationSupport(order = 10)
//    @PostMapping("/export")
//    @ApiImplicitParam(name = "idList", value = "id列表", required = true, paramType = "body", dataType = "List<Long>")
//    @ResponseBody
//    public void exportAlgorithm(HttpServletResponse response, @RequestBody List<Long> idList) {
//        DataAlgorithmPageQueryParamVO algorithmQueryParam = new DataAlgorithmPageQueryParamVO();
//        algorithmQueryParam.setIdList(idList);
//        algorithmQueryParam.setPageSize(10000);
//        PageData<DataAlgorithmBasicVO> data = dataAlgorithmBiz.queryPage(algorithmQueryParam);
//        List<ExcelAlgorithmVO> toList = BeanUtils.copyToList(data.getData(), ExcelAlgorithmVO.class);
//        EasyExcelUtils.exportExcel(response, "算法", toList, ExcelAlgorithmVO.class);
//    }

}
